package com.financialDog.javaTool.controller.accountManage;


import com.alibaba.fastjson.JSONObject;
import com.financialDog.javaTool.controller.reportTools.ReportMergeToolController;
import com.financialDog.javaTool.reportModel.KeyValue;
import com.financialDog.javaTool.services.accountManage.AccountManageService;
import com.financialDog.javaTool.services.fileManage.FileManageService;
import com.financialDog.javaTool.view.account.AccountManageView;
import com.financialDog.javaTool.view.fileManager.FileManageView;
import com.xwintop.xcore.util.javafx.FileChooserUtil;
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
import com.xwintop.xcore.util.javafx.TooltipUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.*;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.util.Callback;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

public class AccountManageController extends AccountManageView {

    public static final String FXML = "/com/financialDog/javaTool/fxmlView/reportTools/AccountManage.fxml";

    private ObservableList<Map<String, String>> tableData = FXCollections.observableArrayList();

    private ReportMergeToolController reportMergeToolController;

    private AccountManageService accountManageService = new AccountManageService(this);

    public static FXMLLoader getFXMLLoader() {
        return new FXMLLoader(AccountManageController.class.getResource(FXML));
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initView();
        initEvent();
        initService();
    }

    private void initView() {
        accountTreeView.setRoot(accountManageService.getRootAccountTree());

        JavaFxViewUtil.setTableColumnMapValueFactory(formulaTableColumn, "formulaTableColumn");
        JavaFxViewUtil.setTableColumnMapAsCheckBoxValueFactory(enableTableColumn, "enableTableColumn",
            (mouseEvent, index) -> {
                accountManageService.setDefaultLine(tableData, index);
                accountTableView.refresh();
            }
        );

        accountTableView.setItems(tableData);
    }

    private void initEvent() {
        //accountTreeView.setCellFactory(TextFieldTreeCell.forTreeView());
        accountTreeView.setOnMouseClicked(event -> {
            tableData.clear();
            TreeItem<KeyValue> selectedItem = accountTreeView.getSelectionModel().getSelectedItem();
            if (selectedItem.getParent().getValue() == null){
                return;
            }
            String key = selectedItem.getValue().getKey();
            accountManageService.setTableData(tableData, key);
        });
    }

    private void initService() {

    }

    public void setReportMergeToolController(ReportMergeToolController reportMergeToolController){
        this.reportMergeToolController = reportMergeToolController;
    }

    public HashMap<KeyValue, String> getTableData(){
        HashMap<KeyValue, String> hashMap = new HashMap<>();
        if (accountTreeView == null){
            accountTreeView = new TreeView<>();
            accountTreeView.setRoot(accountManageService.getRootAccountTree());
        }
        accountTreeView.getRoot().getChildren().forEach(e->{
            String formula = accountManageService.getSelectFormula(e.getValue().getKey());
            hashMap.put(e.getValue(), formula);
        });
        //遍历tree 生成map给与前端
        return hashMap;
    }

    public void setAccountCache(JSONObject accountJsonObject){
        accountManageService.setCacheJsonObject(accountJsonObject);
    }

}
